package com.daybook.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.daybook.pojo.Books;
import com.daybook.pojo.ShopCart;
import com.daybook.pojo.ShopCartAndBooks;
import com.daybook.pojo.User;
import com.daybook.service.ShopCartService;
import com.sun.jndi.url.iiopname.iiopnameURLContextFactory;

@Controller
@RequestMapping(value = "/shopCartMsg",method = RequestMethod.POST)
public class ShopCartController {
	@Autowired
	private ShopCartService shopCartService;
	//1.添加购物车
		@RequestMapping(value = "/addNewCart", method = RequestMethod.POST)
		public void addNewCart(HttpServletRequest request, HttpServletResponse response)
				throws IOException, JSONException {
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			// 获得书的id
			String id = request.getParameter("bid");
			String num=request.getParameter("snum");
			int bid = Integer.valueOf(id);
			int snum=0;
			//当传值为空时（主要用在主页和商品页）默认为+1
			if(num==null||num.equals("")){
				snum=1;
			}else{//主要用于详情页，有数量
				snum=Integer.valueOf(num);
			}
			int result=-1;
			HttpSession session=request.getSession();
  			User user=(User)session.getAttribute("user");
  			if(user!=null){
  				ShopCart shopCart=new ShopCart();
  				shopCart.setUid(user.getUid());
  				shopCart.setBid(bid);
  				shopCart.setSnum(snum);
  				result=shopCartService.addNewCart(shopCart);
  			}else{
  				result=0;
  			}
			PrintWriter out = response.getWriter();
			JSONObject obj = new JSONObject();
			obj.put("res", result);
			out.print(obj);
			out.close();
		}
		
		//2.获取我的购物车信息
		@RequestMapping(value = "/getMyCartList", method = RequestMethod.POST)
		public void getMyCartList(HttpServletRequest request, HttpServletResponse response)
				throws IOException, JSONException {
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			
			int result=-1;
			HttpSession session=request.getSession();
  			User user=(User)session.getAttribute("user");
  			List<ShopCartAndBooks> listCart=new ArrayList();
  			if(user!=null){
  				listCart=shopCartService.getMyCartList(user.getUid());
  			}else{
  				result=0;
  			}
  			if(listCart!=null){
  				result=1;
  			}
			PrintWriter out = response.getWriter();
			JSONObject obj = new JSONObject();
			obj.put("res", result);
			obj.put("list", listCart);
			out.print(obj);
			out.close();
		}
		//3.移除单个商品
		@RequestMapping(value = "/delOneCart", method = RequestMethod.POST)
		public void delOneCart(HttpServletRequest request, HttpServletResponse response)
				throws IOException, JSONException {
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			String id=request.getParameter("sid");
			int sid=Integer.valueOf(id);
			int result=-1;
			HttpSession session=request.getSession();
  			User user=(User)session.getAttribute("user");
  			if(user!=null){
  				result=shopCartService.deleteOneCartById(sid);
  			}else{
  				result=0;
  			}
			PrintWriter out = response.getWriter();
			JSONObject obj = new JSONObject();
			obj.put("res", result);
			out.print(obj);
			out.close();
		}
		//4.移除多个商品
		@RequestMapping(value = "/delCartList", method = RequestMethod.POST)
		public void delCartList(HttpServletRequest request, HttpServletResponse response,Integer[] s_list)
				throws IOException, JSONException {
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			
			HttpSession session=request.getSession();
			int result=-1;
  			User user=(User)session.getAttribute("user");
  			if(user!=null&&s_list!=null){
  			result=shopCartService.deleteCartByListId(s_list);
  			}else{
  				result=0;
  			}
			PrintWriter out = response.getWriter();
			JSONObject obj = new JSONObject();
			obj.put("res", result);
			out.print(obj);
			out.close();
		}
			
				
}
